﻿using UnityEngine;
using System;
using System.Collections.Generic;

public static class DataManager
{

	public static readonly string urlVideo = "http://116.196.107.84/cfg/cfgVideo.txt";
	public static readonly string urlGame = "http://116.196.107.84/cfg/cfgGame.txt";
	public static readonly string urlLesson = "http://116.196.107.84/cfg/cfgLesson.txt";


	public static VideoCfgData videoCfgData = new VideoCfgData ();
	public static GameCfgData gameCfgData = new GameCfgData ();
	public static LessonCfgData lessonCfgData = new LessonCfgData ();

	public static bool Init( Action finishCallback )
	{
		Loading.GetInstance ().StartLoading ();

		int count = 0;
		// read videoCfg
		ResLoader.Load (urlVideo, null, (www) => {

			if ( www.error != null )
			{
				Debug.Log("read cfgVideo error");
			}
			else
			{
				videoCfgData = new VideoCfgData ();
				videoCfgData.Reset (www.text);
			}

			count++;
			if ( count >=3 && finishCallback != null )
			{
				Loading.GetInstance().EndLoading();
				finishCallback();
			}

		}, false, true);


		// read gameCfg
		ResLoader.Load (urlGame, null, (www) => {

			if ( www.error != null )
			{
				Debug.Log("read cfgGame error");
			}
			else
			{
				gameCfgData = new GameCfgData ();
				gameCfgData.Reset (www.text);
			}

			count++;
			if ( count >=3 && finishCallback != null )
			{
				Loading.GetInstance().EndLoading();
				finishCallback();
			}

		}, false, true);


		// read lessonCfg
		ResLoader.Load (urlLesson, null, (www) => {

			if ( www.error != null )
			{
				Debug.Log("read cfgLesson error");
			}
			else
			{
				lessonCfgData = new LessonCfgData ();
				lessonCfgData.Reset (www.text);
			}

			count++;
			if ( count >=3 && finishCallback != null )
			{
				Loading.GetInstance().EndLoading();
				finishCallback();
			}

		}, false, true);


		// unload all resources at last
		Resources.UnloadUnusedAssets();

		return true;

	}

	public static string LoadResourcesCfg( string file )
	{
		TextAsset  txt = (TextAsset)Resources.Load(file);
		//用加载得到的资源对象，实例化游戏对象，实现游戏物体的动态加载

		string v = "";
		if (txt != null)
			v = txt.text;

		return v;
	}
}

